package com.service;

import com.mapper.UserMapper;
import com.po.Course;
import com.po.User;
import com.vo.CourseDTO;
import com.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.contants.PageConstants.PAGE_NUM;
import static com.contants.PageConstants.PAGE_SIZE;

/**
 * Created by 阳幸子 on 2018/4/21.
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CoachService coachService;
    public User login(User user) {
        if (user == null) {
            return null;
        }
        return userMapper.login(user);
    }

    public void register(User user) {
        if (user == null) {
            throw new IllegalArgumentException("user 对象不能为空");
        }
        userMapper.register(user);
    }

    /**
     * 根据用户ID查找对应用户
     *
     * @param userId 用户ID
     */
    public User findById(long userId) {
        if (userId <= 0) {
            return null;
        }
        return userMapper.findById(userId);
    }

    /**
     * 将User对象转化成对应的UserVO对象
     *
     * @param user 即将转化的User对象
     * @return 转化之后的UserVO对象
     */
    public UserVO mapUser2UserVO(User user) {
        if (user == null) {
            throw new IllegalArgumentException("user 对象不能为空");
        } else {
            UserVO userVO = new UserVO();

            userVO.setPhone(user.getPhone());
            userVO.setPhoto(user.getPhoto());
            userVO.setSex(user.isSex());
            userVO.setUserName(user.getUsername());
            userVO.setVipNum(user.getVipnum());
            return userVO;
        }
    }

    /**
     * 获取用户选修的的课程（分页）
     *
     * @param userId   用户ID
     * @param pageNum  页码
     * @param pageSize 页的大小
     * @return List<Course>
     */
    public List<CourseDTO> getCourses(long userId, int pageNum, int pageSize) {
        //页码，页的大小 容错性校验
        pageNum = pageNum <= 0 ? PAGE_NUM : pageNum;
        pageSize = pageSize <= 0 ? PAGE_SIZE : pageSize;

        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;
        if (findById(userId) == null) {
            return null;
        }
        List<Course> courses = userMapper.getCourses(userId, start, end);
        return mapCourse2CourseDTO(courses);
    }




    /**
     * 获取用户选修的的运动事件（分页）
     *
     * @param userId   用户ID
     * @param pageNum  页码
     * @param pageSize 页的大小
     * @return List<Course>
     */
    public List<CourseDTO> getEvents(long userId, int pageNum, int pageSize) {
        //页码，页的大小 容错性校验
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageSize = pageSize <= 0 ? 5 : pageSize;

        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;
        if (findById(userId) == null) {
            return null;
        }
        List<Course> events = userMapper.getEvents(userId, start, end);
        return mapCourse2CourseDTO(events);
    }

    private List<CourseDTO> mapCourse2CourseDTO(List<Course> events) {
        if (events == null || events.isEmpty()) {
            return null;
        }
        int size = events.size();
        List<CourseDTO> courseDTOS = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            CourseDTO courseDTO = new CourseDTO();
            Course course = events.get(i);
            courseDTO.setId(course.getId());
            courseDTO.setCtype(course.getCtype());
            courseDTO.setCourse_type_id(course.getCourse_type_id());
            courseDTO.setCoach_id(course.getCoach_id());
            courseDTO.setCourse_desc(course.getCourse_desc());
            courseDTO.setCourse_name(course.getCourse_name());
            courseDTO.setStart_time(course.getStart_time());
            courseDTO.setEnd_time(course.getEnd_time());
            courseDTO.setLike_count(course.getLike_count());

            String name = coachService.selectById(course.getCoach_id()).getEnglishname();
            courseDTO.setCoachName(name);

            courseDTOS.add(courseDTO);
        }
        return courseDTOS;
    }

    /**
     * 更新用户密码
     *
     * @param userId   changed user Id
     * @param password new password
     */
    public void updatePassword(long userId, String password) {
        if (userId <= 0 || password == null || password.length() <= 0) {
            throw new IllegalArgumentException("update password argument illegal");
        }
        User user = findById(userId);
        if (user == null) {
            return;
        } else {
            userMapper.updatePassword(userId, password);
        }
    }


    public int getTotalCourseNum(){
        return userMapper.getTotalCourseNum();
    }

    public int getTotalEventNum() {
        return userMapper.getTotalEventsNum();
    }
    public List<User> selectAllUser(){
        return userMapper.selectAllUser();
    }
    public int count(){
        return userMapper.count();
    }
    public User selectById(long id){
        return userMapper.findById(id);
    }

    public void updateUser(User user){
        userMapper.updateUser(user);
    }
    public void del_user(long id){
        userMapper.del_user(id);
    }
}
